package com.user.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @GetMapping("/login")
    public Result<?> login(@RequestParam("userId") String userId, @RequestParam("password") String password) {
        // Logic to retrieve user information based on userId
        // For now, returning a dummy result
        //simulate a successful login
        if ("admin".equals(userId) && "123".equals(password)) {
            logger.info("success login userId: {}", userId);
            StpUtil.login(10001);// 10001是用户ID,可从数据库获取
            return Result.success("Login successful for userId: " + userId);
        } else {
            return Result.failed("Invalid userId or password");
        }
    }
    // 查询登录状态，浏览器访问： http://localhost:8090/user/isLogin
    @GetMapping("/isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }
}
